package com.ssyc.gsk_teacher_appraisal.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.view.vp.SuperViewPager;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.base.BaseLazyFragment;
import com.ssyc.gsk_teacher_appraisal.bean.QuestionDetailsInfo;
import com.ssyc.gsk_teacher_appraisal.constant.TeacherAppKeys;

import java.util.ArrayList;
import java.util.List;

/**
 * 完形填空(多题单选)
 * Created by Administrator on 2018/9/3 0003.
 */

public class AppraisalClozeSingleChooseFragment extends BaseLazyFragment implements ViewPager.OnPageChangeListener {

    //服务器返回的页面数据对象
    private QuestionDetailsInfo info;

    //题型
    private TextView tvType;

    //顶部布局
    private LinearLayout llTop;

    //底部布局
    private RelativeLayout rlBottom;

    //标题
    private TextView tvContent;

    //Vp
    private SuperViewPager vp;

    //Vp的指示器
    private TextView tvNum;

    //填充ViewPager的Framgent集合
    private List<Fragment> mFragments;
    //vp的适配器
    private VpCommonAdapter adapter;

    //选项,最多6个
    private String[] letters = {"A", "B", "C", "D", "E", "F"};


    /**
     * 获取当前Fragment对象方法
     *
     * @return
     */
    public static final AppraisalClozeSingleChooseFragment newInstance(QuestionDetailsInfo info) {
        AppraisalClozeSingleChooseFragment appraisalClozeSingleChooseFragment = new AppraisalClozeSingleChooseFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", info);
        appraisalClozeSingleChooseFragment.setArguments(bundle);
        return appraisalClozeSingleChooseFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_teacher_class_cloze_single_choose;
    }


    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
        //初始化视图
        initView(view);

        //获取服务器返回的页面对象
        initIntent();

        //初始化页面数据
        initData();
    }


    /**
     * 获取页面数据
     */
    private void initIntent() {

        //获取服务器返回的页面对象
        info = (QuestionDetailsInfo) getArguments().getSerializable("data");

        if (null != info) {
            //设置题目类型
            tvType.setText(info.getTitle_type());

            //设置题干
            tvContent.setText(info.getContent());
        }

    }


    /**
     * 初始化视图
     */
    private void initData() {
        mFragments = new ArrayList<>();

        if (info != null) {

            //获取所有小题的解析
            String analysis = info.getAnalysis();
            String[] split = analysis.split(TeacherAppKeys.ANALYSIS);

            //获取所有小题的数据集合
            List<QuestionDetailsInfo.SubListBean> sub_list = info.getSub_list();
            if (null != sub_list && sub_list.size() != 0) {
                for (int i = 0; i < sub_list.size(); i++) {
                    //传递小题的数据模型,小题在答题中的位置,小题的解析数据
                    mFragments.add(AppraisalCommonChooseAnswerFragment.newInstance(sub_list.get(i), i, split[i]));
                }
            }

        }

        //设置底部的VP的指示器数字
        tvNum.setText("1/" + mFragments.size());

        //设置ViewPager的数据
        adapter = new VpCommonAdapter(getChildFragmentManager(), mFragments);
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(mFragments.size());
    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvContent = view.findViewById(R.id.tv_content);
        tvType = view.findViewById(R.id.tv_type);
        vp = view.findViewById(R.id.vp);
        vp.addOnPageChangeListener(this);
        tvNum = view.findViewById(R.id.tv_num);
        llTop = view.findViewById(R.id.ll_top);
        rlBottom = view.findViewById(R.id.rl_bottom);
        llTop.setVisibility(View.VISIBLE);
        rlBottom.setVisibility(View.VISIBLE);
    }


    /**
     * Vp的选中事件
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvNum.setText((position + 1) + "/" + mFragments.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}

